package com.kelvin.spiderx.controller;

import com.kelvin.spiderx.common.Common;
import com.kelvin.spiderx.common.ResutlDto;
import com.kelvin.spiderx.common.ResutlTools;
import com.kelvin.spiderx.service.CsdnQcRestService;
import com.kelvin.spiderx.service.DrawService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

/***
 * @title CsdnQcController
 * @desctption CSDN博客服务
 * @author kelvin
 * @create 2023/7/1 14:33
 **/
@RestController
@CrossOrigin("*")
@RequestMapping("/csdn")
@Slf4j
public class CsdnQcController {

    @Resource
    private CsdnQcRestService csdnQcService;

    @Resource
    private DrawService drawService;

    @GetMapping("/draw/url")
    public ResutlDto queryDrawUserList(@RequestParam String url) {
        // 兼容网址简写
        String blogUrl = url;
        if( !blogUrl.startsWith("http") ) {
            blogUrl += "https://";
        }
        return ResutlTools.buildSuccess(drawService.getDrawUserList(blogUrl));
    }

    @GetMapping("/qc/name")
    // 执行查询操作
    public ResutlDto qcbyname(@RequestParam String username ,
                              @RequestParam Integer currentPage) {
        if( currentPage == null ) {
            currentPage = 1;
        }
        return ResutlTools.buildSuccess(csdnQcService.allBlogQcDataByRest(username , currentPage));
    }

    @GetMapping("/qc/clearCache")
    // 执行查询操作
    public ResutlDto qcClearCache(@RequestParam String username) {
        for (int i = 1; i < 4; i++) {
            Common.csdnblogdataCache.remove(username +":"+ i );
        }
        log.info("清理缓存成功, :{}" , username);
        return ResutlTools.buildSuccess("清理缓存成功");
    }

}
